package oss.ngocminh.fw;

import oss.ngocminh.fw.dao.DatabaseHelper;
import oss.ngocminh.fw.dao.SubscriptionDAO;
import oss.ngocminh.fw.dao.ThreadDAO;
import oss.ngocminh.fw.data.Subscription;
import oss.ngocminh.fw.data.Thread;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class SubscriptionEditor extends Activity {

	private static final int RESULT_SUCCESSFUL = 0;
	private static final int RESULT_FAIL = -1;
	
	private DatabaseHelper db;
	private Subscription subscription;
	private SubscriptionDAO subscriptionDAO;
	private ThreadDAO threadDAO;
	private EditText txtBaseUrl;
	private EditText txtThreadId;
	private EditText txtUserName;
	private EditText txtPassword;
	private String savedBaseUrl;
	private String savedThreadId;
	private String savedUserName;
	private String savedPassword;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = new DatabaseHelper(this);
        subscriptionDAO = new SubscriptionDAO(db);
        threadDAO = new ThreadDAO(db);
        
        setContentView(R.layout.activity_subscription_editor);
        txtBaseUrl = (EditText) findViewById(R.id.txtBaseUrl);
        txtThreadId = (EditText) findViewById(R.id.txtThreadId);
        txtUserName = (EditText) findViewById(R.id.txtUserName);
        txtPassword = (EditText) findViewById(R.id.txtPassword);
        
        saveFieldValues();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_subscription_editor, menu);
        return true;
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	db.close();
    }
    
    public void onSave(View view) {
    	if (subscription == null) {
			subscription = new Subscription();
			subscription.setThread(new Thread());
		}
    	subscription.setUserName(val(txtUserName));
    	subscription.setPassword(val(txtPassword));
		if (!val(txtBaseUrl).equals(subscription.getThread().getBaseUrl())
				|| !val(txtThreadId).equals(subscription.getThread().getThreadId())) {
			subscription.getThread().setBaseUrl(val(txtBaseUrl));
			subscription.getThread().setThreadId(val(txtThreadId));
			subscription.getThread().setTitle(null);
			subscription.getThread().setPageCount(0);
		}
		threadDAO.saveOrUpdate(subscription.getThread());
		subscriptionDAO.saveOrUpdate(subscription);
		setResult(RESULT_SUCCESSFUL);
		finish();
    }
    
	public void onReset(View view) {
    	loadFieldValues();
    }
    
    public void onCancel(View view) {
    	setResult(RESULT_FAIL);
		finish();
    }
    
    private void saveFieldValues() {
    	savedBaseUrl = val(txtBaseUrl);
    	savedThreadId = val(txtThreadId);
    	savedUserName = val(txtUserName);
    	savedPassword = val(txtPassword);
    }
    
    private void loadFieldValues() {
    	val(txtBaseUrl, savedBaseUrl);
    	val(txtThreadId, savedThreadId);
    	val(txtUserName, savedUserName);
    	val(txtPassword, savedPassword);
    }
    
    private String val(EditText editText) {
		return editText.getText().toString();
	}

    private void val(EditText editText, String str) {
    	editText.setText(str);
    }
    
}
